/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package YottaTron;


import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import javax.imageio.ImageIO;

/**
 *
 * @author Cenda
 */
public class GraphicsLoader{
    public float alpha;
    GraphicsConfiguration gc;
    int transparency;
     BufferedImage im;
    public ArrayList<Point2D> shape,  originalShape;

      

    public GraphicsLoader(String fileName) throws IOException {
        init();
        im = loadImage(fileName);
        shape = loadShape(fileName);
        originalShape = loadShape(fileName);
               
    }

    public GraphicsLoader(String fileName, String shapeFileName) throws IOException {
        init();
        im = loadImage(fileName);
        shape = loadShape(shapeFileName);
        originalShape = loadShape(shapeFileName);

    }

    public void init() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
//        System.out.println(gc.getColorModel().getPixelSize());
        
    }


//public void getImage(String name){
//
//}
    public BufferedImage loadImage(String fileName) {
        try {
            InputStream is = new FileInputStream(fileName);
            BufferedImage image = ImageIO.read(is);



            transparency = image.getColorModel().getTransparency();
            //upravi inteerni strukturu obrazku pro aktualni graficke zobrazeni
            BufferedImage copy = gc.createCompatibleImage(image.getWidth(), image.getHeight(), transparency);

            Graphics2D g2d = copy.createGraphics();

            g2d.drawImage(image, 0, 0, null);
            g2d.dispose();


            return copy;
        } catch (Exception e) {

            System.out.println("Nepodarilo se nacist obrazek " + fileName);
            return null;

        }

    }

    public ArrayList<Point2D> loadShape(String fileName) throws IOException {
        ArrayList<Point2D> sh = null;
        try {
            sh = new ArrayList<Point2D>();
            //InputStream is = getClass().getResourceAsStream(fileName);
            InputStream is = new FileInputStream(fileName);
            BufferedImage image = ImageIO.read(is);


            for (int i = 0; i < image.getWidth(); i++) {
                for (int j = 0; j < image.getHeight(); j++) {
                    int c = image.getRGB(i, j);
                    int r = c & 0x00ff0000;
                    int g = c & 0x0000ff00;
                    int b = c & 0x000000ff;
                    if ((r == 0) && (g == 0) && (b == 0)) {
                        sh.add(new Point(i, j));

                    //System.out.println("adding point " + i + " " + j + " as a shape");
                    }
                }
            }
//            System.out.println("shape" + fileName +" has  " + sh.size() + " points");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("nepodarilo se nacist obrazek obsahujici Shape");
            throw(new IOException());
        }
        return sh;
    }

    public void rotateShape(double theta) {
        ArrayList<Point2D> sh = new ArrayList<Point2D>();
        int xRot = im.getWidth() / 2;
        int yRot = im.getHeight() / 2;
        int i = 0;
        AffineTransform rotate = AffineTransform.getRotateInstance(theta, xRot, yRot);
        for (Iterator<Point2D> it = originalShape.iterator(); it.hasNext();) {

            sh.add(rotate.transform(it.next(), null));
            //System.out.println("rotating " + originalShape.get(i));
            i++;
        }
        shape = sh;

    }

    //KURVA NAROCNA FUNKCE - zabere hodne casu, chtelo by optimalizovat
    public BufferedImage rotate(double theta, BufferedImage original) {
        BufferedImage copy = gc.createCompatibleImage(original.getWidth(), original.getHeight(), transparency);
        Graphics2D g2 = copy.createGraphics();
        AffineTransform origXform = g2.getTransform();
        AffineTransform newXform = (AffineTransform) (origXform.clone());
        int xRot = original.getWidth() / 2;
        int yRot = original.getHeight() / 2;
        newXform.rotate(theta, xRot, yRot);
        g2.setTransform(newXform);
        g2.drawImage(original, 0, 0, null);
        g2.setTransform(origXform);
        return copy;
    }

    public BufferedImage getImage(double angle) {
        return im;
    }

    public ArrayList<Point2D> getShape() {
        return shape;
    }

   
}
